package com.example;

import java.util.*;

/* 扑克类 */
public class Poker {
    /* 存牌的花色 */
    private static final List<String> colors = new ArrayList<>();
    /* 存牌的数字 */
    private static final List<String> numbers = new ArrayList<>();
    /* 存原始牌：牌的序号 + 牌的内容 */
    /* 牌的序号便于后续做排序理牌操作 */
    private static final Map<Integer, String> cards = new LinkedHashMap<>();
    /* 玩家1的手牌 */
    private static final List<String> player1 = new ArrayList<>();
    /* 玩家2的手牌 */
    private static final List<String> player2 = new ArrayList<>();
    /* 玩家3的手牌 */
    private static final List<String> player3 = new ArrayList<>();
    /* 地主的底牌 */
    private static final List<String> landCard = new ArrayList<>();

    /* 静态域：随着类加载而执行 */
    static {
        /* 存放花色 */
        Collections.addAll(colors, "♥", "♦", "♠", "♣");
        /* 存放数字 */
        Collections.addAll(numbers, "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2");
        /* 组牌 */
        int index = 1;
        for (String number : numbers) {
            for (String color : colors) {
                cards.put(index++, color + number);
            }
        }
        cards.put(index++, "小☻");
        cards.put(index++, "大☺");
    }

    /**
     * 1.组牌：做好原始的牌存入一个集合
     * 2.洗牌：打乱原始牌的集合
     * 3.发牌：将打乱的牌分发到4个集合
     * 4.理牌：将4个集合做升序排序
     * @param args
     */
    public static void main(String[] args) {
        randomCard();
        System.out.println(player1);
        System.out.println(player2);
        System.out.println(player3);
        System.out.println(landCard);
    }

    /* 洗牌 */
    private static void randomCard() {
        /* 取牌号 */
        Set<Integer> integers = cards.keySet();
        /* 牌号是升序的顺序 */
        List<Integer> keys = new ArrayList<>(integers);
        /* 打乱牌的顺序 */
        Collections.shuffle(keys);
        /* 存玩家拿到的牌号 */
        List<Integer> playNumber1 = new ArrayList<>();
        List<Integer> playNumber2 = new ArrayList<>();
        List<Integer> playNumber3 = new ArrayList<>();
        List<Integer> landNumber = new ArrayList<>();
        for (int i = 0; i < keys.size(); i++) {
            /* 根据i去分配牌 */
            if (i >= 51) {
                landNumber.add(keys.get(i));
            } else if (i % 3 == 0) {
                /* i:0,3,6...48:第一个玩家; */
                playNumber1.add(keys.get(i));
            } else if (i % 3 == 1) {
                /* i:1,4,7...49:第二个玩家; */
                playNumber2.add(keys.get(i));
            } else {
                /* i:2,5,8...50:第三个玩家; */
                playNumber3.add(keys.get(i));
            }
        }
        /* 理牌 */
        /* 根据玩家的牌号去做升序 */
        Collections.sort(playNumber1);
        Collections.sort(playNumber2);
        Collections.sort(playNumber3);
        Collections.sort(landNumber);
        /* 根据理完的牌号存入相应的牌 */
        insertCard(playNumber1, player1);
        insertCard(playNumber2, player2);
        insertCard(playNumber3, player3);
        insertCard(landNumber, landCard);
    }

    /**
     * 根据理完的牌号存入相应的牌
     * @param keys 玩家的牌号集合
     * @param player 玩家的牌集合
     */
    private static void insertCard(List<Integer> keys, List<String> player) {
        for (Integer key : keys) {
            player.add(cards.get(key));
        }
    }
}